package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblDKMHKetquaService;
import com.lb.jwitter.service.TblDKMHLogKetQuaDangKy;
import com.lb.jwitter.service.TblDKMHLogKetQuaDangKyService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.QuyenDangKy;
import com.lb.jwitter.util.ThoiKhoaBieuDuKien;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations = "classpath:beans.xml")
public class BackKetQuaDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;

	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private int soTietTrongNgay;

	public int getSoTietTrongNgay() {
		return soTietTrongNgay;
	}

	private List<KetQuaDangKy> ketQuaDangKys;

	public List<KetQuaDangKy> getKetQuaDangKys() {
		return ketQuaDangKys;
	}

	public void setKetQuaDangKys(List<KetQuaDangKy> ketQuaDangKys) {
		this.ketQuaDangKys = ketQuaDangKys;
	}

	private int ketquaId;

	public int getKetquaId() {
		return ketquaId;
	}

	public void setKetquaId(int ketquaId) {
		this.ketquaId = ketquaId;
	}

	private TblCTDTDMTietHocService tblCTDTDMTietHocService;

	private String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	private String code;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien = (TblDKMHTaiKhoanHocVien) session
				.get("taiKhoanHocVien");
		if (tblDKMHTaiKhoanHocVien == null)
			return "noprivilege";
		

		Integer dotDKId = (Integer) session.get("dotDKId");

		ApplicationContext context = contextProvider.getApplicationContext();

		tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context
				.getBean("tbl_CTDT_DM_TietHocClient");
		TblDKMHKetquaService tblDKMHKetquaService = (TblDKMHKetquaService) context
				.getBean("tbl_DKMH_KetquaClient");
		TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService = 
				(TblThongTinNhomLopMonHocService)  context.getBean("tbl_ThongTin_NhomLop_MonHocClient");
		
		TblThongTinTKBChiTietService tblThongTinTKBChiTietService = 
				(TblThongTinTKBChiTietService)  context.getBean("tbl_ThongTin_TKB_ChiTietClient");
		
		
		ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
		boolean trongHanDK = (Boolean) session.get("trongHanDK");
		if (trongHanDK) {
			for (KetQuaDangKy ketqua : ketQuaDangKys) {
				if (ketqua.getKetqua().getId().equals(new Long(ketquaId))) {
					//KhoaMonHoc - KX:khoa khong cho huy va hieu chinh
					if (!ketqua.getKetqua().getKhoaMonHocYN().equals("KX")) {
						try {
						    TblThongTinNhomLopMonHoc tblThongTinNhomLopMonHoc = 
						    		tblThongTinNhomLopMonHocService
						    		.gettblThongTinNhomLopMonHoc(new Long(ketqua.getKetqua().getMaDieuChinh2()));
						    
						    if(tblThongTinNhomLopMonHoc != null){

						    	List<ConditionReport> cons = new ArrayList<ConditionReport>();
							    { 
							        ConditionReport con = new ConditionReport(); 
							        con.setCol("TKBId.id");
							        con.setValue1(tblThongTinNhomLopMonHoc.getTKBId().getId());
							        con.setOperator("eq");
							        cons.add(con);	
							    }
							    { 
							        ConditionReport con = new ConditionReport(); 
							        con.setCol("ngayHocId.STT");
							        con.setValue1("asc");
							        con.setOperator("order");
							        cons.add(con);	
							    }
							    ketqua.getThoiKhoaBieuDuKien().setNhomLop(tblThongTinNhomLopMonHoc);
						    	ketqua.getThoiKhoaBieuDuKien().setChiTiets(tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons, 0, 0));
						    	ketqua.getKetqua().setNLMHId(tblThongTinNhomLopMonHoc);
						    	
						    	ketqua.getKetqua().setMaDieuChinh2("");
						    	ketqua.getKetqua().setKetQua(ketqua.getKetqua().getKetQua2());
						    	ketqua.getKetqua().setKetQuaLyDo(ketqua.getKetqua().getKetQuaLyDo2());
						    	ketqua.getKetqua().setKetQua2("");
						    	ketqua.getKetqua().setKetQuaLyDo2("");
						    	ketqua.getKetqua().setXacNhanYN(true);
						    	
						    	tblDKMHKetquaService.updatetblDKMHKetqua(ketqua.getKetqua());
						    	
						    }else{
						    	code = "ERROR";
								msg = "Trạng thái quay lại " +ketqua.getKetqua().getMaDieuChinh2() +" không tồn tại, vui lòng liên hệ ban quản trị!!!";
								return Action.ERROR;
						    }
						} catch (Exception e) {
							// TODO: handle exception
							code = "ERROR";
							msg = "Trạng thái quay lại " +ketqua.getKetqua().getMaDieuChinh2() +" không tồn tại, vui lòng liên hệ ban quản trị!!!";
							return Action.ERROR;
						}
					}else{
						code = "ERROR";
						msg = " Kh\u00F3a kh\u00F4ng cho hi\u1EC7u ch\u1EC9nh v\u00E0 h\u1EE7y m\u00F4n h\u1ECDc, M\u00F4n h\u1ECDc c\u1EE9ng!!!";
						return Action.ERROR;
					}//if khoamonhocYN KX
					
					break;
				}//end-if ketqua == ketquaId
			}//end-for
			
		}
		//het han dang ky
		else {
			
			code = "ERROR";
			//het han huy mon hoc
			msg = "Đã hết hạn!!!";
			return Action.ERROR;
		}//end-if trongHanDK && Delete = true
					
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
    	{ 
	        ConditionReport con = new ConditionReport(); 
	        con.setCol("ma");
	        con.setValue1("0");
	        con.setOperator("ne");
	        cons.add(con);	
	   }
	    soTietTrongNgay = tblCTDTDMTietHocService.counttblCTDTDMTietHocByCondition(cons);

		session.put("ketQuaDangKys", ketQuaDangKys);
		return Action.SUCCESS;

	}

}
